from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from django.core.paginator import Paginator
from users.ser import *
from users.models import *
import re
from utils.comm import send_message, get_redis
import random


# Create your views here.

# class Test(APIView):
#     def get(self, request):
#         conn = get_redis()
#         conn.setex('username', 60, 'xiaoming')
#         name = conn.get('username')
#         return Response(name)


class SendSms(APIView):
    def post(self, request):
        mobile = request.data.get('mobile')
        codesms = random.randint(100000, 999999)
        res = send_message(mobile, str(codesms), 1)
        code = 10010
        if res:
            code = 200
            # 发送验证码成功后存入redis
            conn = get_redis()
            conn.setex(mobile, 60, codesms)
        return Response({'code': code})


# 注册接口
class Register(APIView):
    def post(self, request):

        username = request.data.get('username')
        # 验证用户名是否为空
        if not username:
            return Response({'code': '10010', 'msg': '用户名不能为空'})
        # 格式是否正确
        name = re.match('\w{5,10}$', username)
        if not name:
            return Response({'code': '10011'})
        # 是否唯一
        user = User.objects.filter(username=username).first()
        if user:
            return Response({'code': '10012'})

        # 验证密码是否为空
        password = request.data.get('password')
        if not password:
            return Response({'code': '10013', 'msg': '密码不能为空'})
        # 密码格式是否正确
        pd = re.match('\w{8,20}$', password)
        if not pd:
            return Response({'msg': '密码格式不对', 'code': '10014'})

        # 手机号
        mobile = request.data.get('mobile')
        # 验证用户名是否为空
        if not mobile:
            return Response({'code': '10015', 'msg': '手机号不能为空'})
        # 格式是否正确
        phone = re.match('^1[3-9]\d{9}$', mobile)
        if not phone:
            return Response({'code': '10016'})
        # 是否唯一
        sloe = User.objects.filter(mobile=mobile).first()
        if sloe:
            return Response({'code': '10017'})

        # 验证码验证 newcode，redis取，根据手机号
        smscode = request.data.get('smscode')
        conn = get_redis()
        code = conn.get(mobile)
        if code:
            code = str(code, encoding='utf8')
        if smscode != code:
            return Response({'code': '10018'})
        return Response({'code': 200, 'msg': '注册成功'})
# class NewsAPIView(APIView):
#     def get(self, request):
#         id = request.GET.get('id')
#         data = News.objects.all()
#         page = Paginator(data, 2)
#         page_all = page.page(id)
#         data_all = NewsModelSerializer(page_all, many=True)
#
#         return Response({
#             'data': data_all.data,
#             'page_range': [i for i in page.page_range],
#             'count': page.count
#         })
#
#     def post(self, request):
#         data = NewsModelSerializer(data=request.data)
#         if data.is_valid():
#             data.save()
#             return Response({'msg': '添加成功', 'code': 200})
#         else:
#             return Response({'msg': '添加失败'})


# class AdvertisingAPIView(APIView):
#     def get(self, request):
#         id = request.GET.get('id')
#         data = Advertising.objects.all()
#         page = Paginator(data, 2)
#         page_all = page.page(id)
#         data_all = AdvertisingModelSerializer(page_all, many=True)
#
#         return Response({
#             'data': data_all.data,
#             'page_range': [i for i in page.page_range]
#
#         })
#
#     def post(self, request):
#         data = AdvertisingModelSerializer(data=request.data)
#         if data.is_valid():
#             data.save()
#             return Response({'msg': '添加成功', 'code': 200})
#         else:
#             return Response({'msg': '添加失败'})
